/**
 * 
 */
package com.wolfpire.proms.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.service.TaskService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.DataDictService;

/**
 * 任务控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/task")
public class TaskController {
	
	@Resource TaskService taskService;
	
	@Resource DataDictService dataDictService;
	
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("proms/task/task_list", "operAuthorities", operAuthorities);
	}

	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<Task> queryList(EasyuiPage<Task>page, Task filterTask) {
		// TODO: 是否考虑用VO
		Page<Task> tasks = this.taskService.setPageDataList(page, filterTask);
		return new EasyuiPagination<Task>(tasks);
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, Task task) {
		ModelAndView mv = new ModelAndView("proms/task/task_add");
		mv.getModelMap().addAttribute(task);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Result save(Task task) {
		try {
			this.taskService.saveOrUpdate(task);
			return new Result(true, "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败");
		}
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("proms/task/task_edit");
		if (null != id) {
			Task task = this.taskService.get(id);
			mv.addObject(task);
		}
		return mv;
	}
	
	@RequestMapping("/detail")
	public ModelAndView detail(
			@PrincipalArgument User user,
			@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "type", required = false) String type) {
		ModelAndView mv = new ModelAndView("proms/task/task_detail");
		try {
			if (null != id) {
				Task task = this.taskService.getProjectIndexInfoTask(id);
				mv.addObject(task);
			}
			if (!StringUtils.isBlank(type)) {
				mv.getModelMap().put("type", type);
			} else {
				mv.getModelMap().put("type", StringUtils.EMPTY);
			}
			DataDict isTaskPlan = this.dataDictService.findUniqueByCode(Constants.DATA_DICT_CODE_TASKPLAN);
			if (null != isTaskPlan) {
				mv.getModelMap().put("isTaskPlan", isTaskPlan.getRemark());
			} else {
				mv.getModelMap().put("isTaskPlan", StringUtils.EMPTY);
			}
			/*DataDict dataDict = this.dataDictService.findUniqueByCode(Constants.FEEDBACK_AUDIT_ROLE);
			String roleIdStr = dataDict.getRemark();
			Long[] userRoles = user.getRoleIds();
			if (ArrayUtils.contains(userRoles, Long.parseLong(roleIdStr))) {
				mv.getModelMap().put("isAudit", "1");
			} else {
				mv.getModelMap().put("isAudit", StringUtils.EMPTY);
			}*/
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
	
	@RequestMapping("/detailTabs")
	public ModelAndView detailTabs(
			@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "type", required = false) String type) {
		ModelAndView mv = new ModelAndView("proms/task/task_detail_tabs");
		try {
			mv.getModelMap().put("biz_id", id);
			if (!StringUtils.isBlank(type)) {
				mv.getModelMap().put("type", type);
			} else {
				mv.getModelMap().put("type", StringUtils.EMPTY);
			}
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
	
	@RequestMapping("/attas")
	public ModelAndView attas(
			@RequestParam(value = "id", required = true) Long id) {
		ModelAndView mv = new ModelAndView("proms/task/task_attas");
		try {
			mv.getModelMap().put("biz_id", id);
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(@RequestParam(value="id")Long id) {
		try {
			this.taskService.del(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@RequestMapping("/move")
	public ModelAndView move(Task task) {
		ModelAndView mv = new ModelAndView("proms/task/task_move");
		if (null != task.getId()) {
			task = this.taskService.getProjectIndexInfoTask(task.getId());
			mv.addObject(task);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/moveTask")
	public Result moveTask(@RequestParam("id") Long id,
			@RequestParam("projectIndexId") Long projectIndexId,
			@RequestParam("targetProjectIndexId") Long targetProjectIndexId) {
		try {
			this.taskService.move(id, projectIndexId, targetProjectIndexId);
			return new Result(true, "迁移成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "迁移失败，请联系管理员");
		}
	}
	
	@RequestMapping("/taskFeedbackAttas")
	public ModelAndView taskFeedbackAttas() {
		return new ModelAndView("proms/task/task_feedback_attas");
	}
	
	@RequestMapping("/myList")
	public ModelAndView myList(@PrincipalArgument User user,
			@RequestParam Long menuId,
			@RequestParam(value = "page", required = false) String page) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		String viewName = "proms/task/task_myList";
		if (StringUtils.isNotBlank(page)) {
			viewName = viewName + "_" + page;
		}
		 ModelAndView mv = new ModelAndView(viewName, "operAuthorities", operAuthorities);
		 mv.getModel().put("userId", user.getId());
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/queryMyList")
	public EasyuiPagination<Map<String, Object>> queryMyList(
			EasyuiPage<Map<String, Object>> page,
			Task filterTask,
			@PrincipalArgument User user) {
		filterTask.setTaskActorIds(user.getId().toString());
		filterTask.setCreateUserId(user.getId());
		Page<Map<String, Object>> tasks = this.taskService.setPageMyDataList(page, filterTask);
		return new EasyuiPagination<Map<String, Object>>(tasks);
	}
	

	@RequestMapping("/feedbackTabs")
	public ModelAndView taskFeedbackTabs(
			@PrincipalArgument User user,
			@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "type", required = false) String type) {
		ModelAndView mv = new ModelAndView("proms/task/task_feedback_tabs");
		try {
			if (null != id) {
				Task task = this.taskService.getProjectIndexInfoTask(id);
				mv.addObject(task);
			}
			if (!StringUtils.isBlank(type)) {
				mv.getModelMap().put("type", type);
			} else {
				mv.getModelMap().put("type", StringUtils.EMPTY);
			}
			DataDict isTaskPlan = this.dataDictService.findUniqueByCode(Constants.DATA_DICT_CODE_TASKPLAN);
			if (null != isTaskPlan) {
				mv.getModelMap().put("isTaskPlan", isTaskPlan.getRemark());
			} else {
				mv.getModelMap().put("isTaskPlan", StringUtils.EMPTY);
			}
			/*DataDict dataDict = this.dataDictService.findUniqueByCode(Constants.FEEDBACK_AUDIT_ROLE);
			String roleIdStr = dataDict.getRemark();
			Long[] userRoles = user.getRoleIds();
			if (ArrayUtils.contains(userRoles, Long.parseLong(roleIdStr))) {
				mv.getModelMap().put("isAudit", "1");
			} else {
				mv.getModelMap().put("isAudit", StringUtils.EMPTY);
			}*/
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
	
	@RequestMapping("/assign")
	public ModelAndView assign(HttpSession session) {
		ModelAndView mv = new ModelAndView("proms/task/task_add");
		return mv;
	}
	
	@RequestMapping("/taskMgr")
	public ModelAndView taskMgr(
			@PrincipalArgument User user,
			@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "type", required = false) String type,
			@RequestParam(value = "oper", required = false) String oper) {
		ModelAndView mv = new ModelAndView("proms/task/task_mgr");
		mv.getModelMap().put("userId", user.getId());
		try {
			if (null != id) {
				Task task = this.taskService.getProjectIndexInfoTask(id);
				mv.addObject(task);
			}
			if (!StringUtils.isBlank(type)) {
				mv.getModelMap().put("type", type);
			} else {
				mv.getModelMap().put("type", StringUtils.EMPTY);
			}
			DataDict isTaskPlan = this.dataDictService.findUniqueByCode(Constants.DATA_DICT_CODE_TASKPLAN);
			if (null != isTaskPlan) {
				mv.getModelMap().put("isTaskPlan", isTaskPlan.getRemark());
			} else {
				mv.getModelMap().put("isTaskPlan", StringUtils.EMPTY);
			}
			
			if(oper!=null){
				if(oper.indexOf("planMgr")>0){
					mv.getModelMap().put("planMgr", 1);
				}
				if(oper.indexOf("taskMgr")>0){
					mv.getModelMap().put("taskMgr", 1);
				}
			}
			/*DataDict dataDict = this.dataDictService.findUniqueByCode(Constants.FEEDBACK_AUDIT_ROLE);
			String roleIdStr = dataDict.getRemark();
			Long[] userRoles = user.getRoleIds();
			if (ArrayUtils.contains(userRoles, Long.parseLong(roleIdStr))) {
				mv.getModelMap().put("isAudit", "1");
			} else {
				mv.getModelMap().put("isAudit", StringUtils.EMPTY);
			}*/
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
}
